package cn.bingo.yunsj.update;

import cn.bingo.yunsj.domain.RpModel;
import cn.bingo.yunsj.common.exception.EmException;
import cn.bingo.yunsj.common.exception.OwnException;
import cn.bingo.yunsj.mapper.RpModelMapper;
import cn.bingo.yunsj.model.Model;
import cn.bingo.yunsj.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 模板信息更新
 */
@Component
public class ModelUpdate {

    @Autowired
    private RpModelMapper modelMapper;

    private static final Logger logger= LoggerFactory.getLogger(ModelUpdate.class);

    public void doModelUpdate(){
        ModelUtils modelUtils = new ModelUtils();
        List<Model> file = modelUtils.getFile();
        System.out.println(file);
        file.forEach(e->{
            RpModel rpModel = new RpModel();
            BeanUtils.copyProperties(e,rpModel);
            rpModel.setCreatedTime(new Date());
            rpModel.setUpdatedTime(new Date());
            rpModel.setCreatedBy("HR");
            rpModel.setUpdatedBy("HR");
            rpModel.setModelId(UUID.randomUUID().toString());
            try{
                modelMapper.insert(rpModel);
            }catch (DuplicateKeyException k){
                throw new OwnException(EmException.DK_EXCPTION);
            }
        });
    }
}
